package model.preprocessor;

import java.io.BufferedWriter;
import java.io.IOException;

import model.BasicResult;
import model.cnf.CNF;
import controller.xml.XMLBaliseFormat;

/**
 * Represent the result of a preprocessor execution. It provide some useful information as the
 * status of the execution or stdin and stderr.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class PreProcessorResult extends BasicResult {

	public static final String						DEFAULT_DESCRIPTION	= "Result of the preprocessing";
	/** the default status of a preprocessor execution */
	public static final PreProcessorResultStatus	DEFAULT_STATUS		= PreProcessorResultStatus.OK;

	private PreProcessorResultStatus				status				= DEFAULT_STATUS;
	private CNF										cnf					= null;
	private String									exportPath			= "";

	/**
	 * Create a new preprocessor result.
	 */
	public PreProcessorResult() {
		description = DEFAULT_DESCRIPTION;
	}

	/**
	 * Create a new preprocessor result.
	 * 
	 * @param basic
	 *            the {@link BasicResult}
	 */
	public PreProcessorResult(BasicResult basic) {
		description = basic.getDescription();
		memoryUsed = basic.getMemoryUsed();
		begin = basic.getBegin();
		end = basic.getEnd();
		exitVal = basic.getExitVal();
		stderr = basic.getStderr();
		stdout = basic.getStdout();
	}

	/**
	 * Export the data in XML
	 * 
	 * @param file
	 *            {@link BufferedWriter} to write
	 * @throws IOException
	 */
	@Override
	public void exportXML(BufferedWriter file) throws IOException {

		// open
		// file.newLine();
		file.write("<" + XMLBaliseFormat.PREPROC_RESULT + ">");

		// basic
		super.exportXML(file);

		// status
		// file.newLine();
		file.write("<" + XMLBaliseFormat.PREPROC_RESULT_STATUS + ">");
		file.write(status.getXmlValue());
		file.write("</" + XMLBaliseFormat.PREPROC_RESULT_STATUS + ">");

		// close
		// file.newLine();
		file.write("</" + XMLBaliseFormat.PREPROC_RESULT + ">");
	}

	/**
	 * 
	 * @return the path of the output file.
	 */
	public String getExportPath() {
		return exportPath;
	}


	/**
	 * 
	 * Set the path of the output file.
	 */
	public void setExportPath(String exportPath) {
		this.exportPath = exportPath;
	}

	/**
	 * 
	 * @return the {@link CNF} resulting of the preprocessor execution.
	 */
	public CNF getCnf() {
		return cnf;
	}

	/**
	 * 
	 * Set the {@link CNF} resulting of the preprocessor execution.
	 */
	public void setCnf(CNF cnf) {
		this.cnf = cnf;
	}

	/**
	 * 
	 * @return the status of the execution {@link PreProcessorResultStatus}
	 */
	public PreProcessorResultStatus getStatus() {
		return status;
	}


	/**
	 * 
	 * Set the status of the execution {@link PreProcessorResultStatus}
	 */
	public void setStatus(PreProcessorResultStatus status) {
		this.status = status;
	}

	@Override
	public String toString() {
		String res = description + " : \nStatus: " + status + "\n" + "Preprocessing time: " + getExecutionTime() + "\nwith begin at : "
				+ begin + " ; end at :" + end;

		if (cnf == null || cnf.getStat() == null)
			res += "\nno statistic available";
		else
			res += "\n" + cnf.getStat().toString();

		return res;
	}

}
